include	Irvine32.inc
KEY = 239 ;1-255 区间
BUFMAX = 128 ; 缓冲区最大值
ARRAY_LENGTH = 2

_ENTER_KEY = 0DH

.data
	sPrompt		byte	"Enter the plain textequ:",0
	sEncrypt	byte	"Cipher textequ: ",0
	sDecrypt	byte	"Decrypted: ",0
	buffer		byte	BUFMAX + 1 dup (0)
	bufSize		dword	?
	inputStr	byte	2 + 1 dup (0)
	InvalidInputMsg byte "Invalid input",0DH,0AH,0
	Quit_msg byte	"Enter anyKey to Quit....",0
	;字符串指针数组
	ConditionalArray	dword	ARRAY_LENGTH dup (0)
	str_L1 		byte	"L1",0
	str_L2		byte	"L2",0
	
.code
main PROC
	


	mov 	edx,offset	inputStr
	mov		ecx,3
	;call	ReadString
	call	init_strArr

	call	cmp_str
	jmp		main_Quit
	;---------------
	; X = ((X ^ Y) ^ Y)
	; 双层异或 对称加解密
	call InputTheString		;输入明文
	call Translatebuffer	;加密缓冲区
	mov  edx,offset sEncrypt
	call DisplayMessage		;显示加密消息
	call TranslateBuffer	;解密缓冲区
	mov  edx,offset sEncrypt
	call DisplayMessage
	;-----------------------

;-------------------------------
;	(FSM)
L1:	
	call	Finite
	jmp		main_Quit
;-------------------------------

main_Quit:
	call pause_i
	call	Crlf
	exit
main ENDP	

;-------------------------------
;	COMMENT FUNCTION
pause_i PROC USES esi ecx
;	暂停函数
	mov 	edx,offset	Quit_msg
	call	WriteString
	call	ReadChar
	ret
pause_i ENDP

init_strArr PROC
;	初始化字符串指针数组
	pushad
	mov		ecx,ARRAY_LENGTH
	mov 	esi,0
	mov 	edi,offset	str_L1
L1:
	mov 	ConditionalArray[esi * type dword],edi
	inc		esi
;	需要将edi逐步后移到下一个字符串的开头位置(上一个字符串结尾0的后一位)
L2:
	mov 	al, byte ptr [edi]	;edi取32位(4字节)信息，我们需要诸葛字节比较取8位
	cmp		al,0
	je		L3		;等于0说明在字符串结尾
	inc		edi		;未到字符串结尾
	jmp		L2		;对比余下的字符
L3:
	inc		edi		;执行下一个字符串的起始位置
	loop	L1		;循环处理之后的字符串
	popad	
	ret
init_strArr ENDP

cmp_str	PROC
	pushad	
	


	popad
	ret
cmp_str	ENDP
;-------------------------------

;----------------------------------
InputTheString proc
; 提示输入字符串
; 保存字符串和长度
; 接受: None
; 返回: None
	pushad   ;寄存器依次入栈
	mov		edx,offset sPrompt
	call 	WriteString
	mov 	ecx,BUFMAX
	mov 	edx,offset buffer
	call 	ReadString
	mov 	bufSize,eax
	call 	Crlf
	popad
	ret
InputTheString ENDP
;----------------------------------


;-----------------------------------
DisplayMessage PROC
; 显示加密或解密字符串
; 接受: edx指向的消息
; 返回: None
	pushad
	call 	WriteString
	mov 	edx,offset buffer
	call	WriteString
	call	Crlf
	call	Crlf
	popad
	ret
DisplayMessage ENDP
;----------------------------------

;-----------------------------------
TranslateBuffer PROC
; 字符串的每个字符都是密钥字符进行异或操作
; 接受: None 
; 返回: None
	pushad
	mov 	ecx,bufSize
	mov 	esi,0
L1:
	xor		buffer[esi],KEY
	inc		esi
	loop	L1
	popad
	ret
TranslateBuffer ENDP
;----------------------------------

;----------------------------------
Finite PROC
;	有限状态机(FSM)
;	Finite
	pushad
	call	clrscr
StateA:
	call	Getnext
	cmp		al,	'+'
	je		StateB
	cmp		al,	'-'
	je		StateB
	call	IsDigit
	jz		StateC
	call	DisplayErrorMsg
	jmp		Quit
StateB:
	call	Getnext
	call	IsDigit
	jz		StateC
	call	DisplayErrorMsg
	jmp		Quit
StateC:
	call	Getnext
	call	IsDigit
	jz		StateC
	cmp		al,	_ENTER_KEY
	je		Quit
	call	DisplayErrorMsg
Quit:
	call	Crlf
	popad
	ret
Finite ENDP

Getnext PROC 
;	从标准输入读取一个字符
;	param: 	None
;	return:	al
	call	ReadChar
	call	WriteChar
	ret
Getnext ENDP

DisplayErrorMsg	PROC
;	显示一个错误消息
;	输入流中包含非法输入
;	param:	None
;	return:	None
	push 	edx
	mov 	edx,	offset InvalidInputMsg
	call	WriteString
	pop 	edx
	ret
DisplayErrorMsg ENDP
;----------------------------------
END	main



	


